package com.otod.sys.amcm.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.otod.base.json.JsonFailure;
import com.otod.base.json.JsonSuccess;
import com.otod.commons.auth.SessionUserHolder;
import com.otod.commons.auth.SessionUserUtil;
import com.otod.commons.constants.CommonConstant;
import com.otod.commons.entity.Page;
import com.otod.commons.utils.StringUtils;
import com.otod.commons.web.BaseController;
import com.otod.sys.amcm.bo.AmcmMsgAppBo;
import com.otod.sys.amcm.service.AmcmMsgAppService;
import com.otod.sys.vo.AmcmMsgAppVo;

/**
 * 消息
 * @author long
 *
 */
@Controller
@RequestMapping("sys/amcm")
public class AmcmMsgAppController extends BaseController {
	@Autowired
	AmcmMsgAppService amcmMsgAppService;
	
	

	/**
	 * 得到登录用的UserNo 
	 * @return
	 */
	private static String getUserNo(){
		SessionUserHolder sessionUser = SessionUserUtil.getCurrentUser();
		String userNo= sessionUser.getUserNo(); //得到登录用的UserNo 
		return userNo;
	}
	
	/**
	 * 查询所有的消息记录
	 * @return
	 */
	@RequestMapping(value="queryForlist",method= RequestMethod.GET)
	@ResponseBody
	public List<AmcmMsgAppVo> queryForlist(){
		List<AmcmMsgAppVo> list = Lists.newArrayList();
		Map<String, Object> map = new HashMap<>();
		map.put("empNo", getUserNo());
		list = amcmMsgAppService.queryForlist(map);	
		return list;
	}
	/**
	 * 查询未读的消息
	 * @return
	 */
	@RequestMapping(value="queryForlistNoRead",method= RequestMethod.GET)
	@ResponseBody
	public AmcmMsgAppBo queryForlistNoRead(String isRead){
		AmcmMsgAppBo amcmMsgAppBo = new AmcmMsgAppBo();
		List<AmcmMsgAppVo> list = Lists.newArrayList();
		Map<String, Object> map = new HashMap<>();
		map.put("empNo", getUserNo());
		map.put("isRead", isRead);
		list = amcmMsgAppService.queryForlist(map);	
		amcmMsgAppBo.setMessageNumber(list.size());
	
		//添加三条数据
		List<AmcmMsgAppVo> amcmMsgAppVos = Lists.newArrayList();
		for (int i = 0; i < list.size(); i++) {
		
			if(i<=3){
				amcmMsgAppVos.add(list.get(i));
			}
			if(i==3){
				break;
			}
		}
		amcmMsgAppBo.setMsgAppVos(amcmMsgAppVos);
		return amcmMsgAppBo;
		
	}
	/**
	 * 查询已读的消息
	 * @return
	 */
	@RequestMapping(value="queryForlistYesRead",method= RequestMethod.GET)
	@ResponseBody
	public AmcmMsgAppBo queryForlistYesRead(){
		AmcmMsgAppBo amcmMsgAppBo = new AmcmMsgAppBo();
		List<AmcmMsgAppVo> list = Lists.newArrayList();
		Map<String, Object> map = new HashMap<>();
		map.put("empNo", getUserNo());
		map.put("isRead", CommonConstant.COMMON_YES);
		list = amcmMsgAppService.queryForlist(map);	
		amcmMsgAppBo.setMessageNumber(list.size());
		
		//添加三条数据
		List<AmcmMsgAppVo> amcmMsgAppVos = Lists.newArrayList();
		for (int i = 0; i < list.size(); i++) {
			if(i<=3){
				amcmMsgAppVos.add(list.get(i));
			}
			if(i==3){
				break;
			}
			
		}
		amcmMsgAppBo.setMsgAppVos(amcmMsgAppVos);
	
		return amcmMsgAppBo;
	}
	
	
	/**
	 * 分页查询未阅读的信息
	 * @param request
	 * @param prj_name
	 * @return
	 */
	@RequestMapping(value="queryPageRead",method=RequestMethod.GET)
	@ResponseBody
	public Page queryPageRead(HttpServletRequest request,String isRead){
		
		List<AmcmMsgAppVo> list = Lists.newArrayList();
		Map params = super.getParamMapFromRequest(request);
		params.put("empNo", getUserNo());
		params.put("isRead", isRead);
		Page page = new Page(request);
		return amcmMsgAppService.queryPageForList(page, params);
	}
	

	/**
	 * 通过uuid查询数据要读的消息 并且把消息状态修改为已读  
	 * @param uuid
	 * @retur
	 */
	@RequestMapping(value = "getAmcmMsgApp",method = RequestMethod.GET)
	@ResponseBody
	public AmcmMsgAppVo getAmcmMsgApp(String uuid){
		AmcmMsgAppVo amcmMsgAppVo = null;
		if(!StringUtils.isEmpty(uuid)){
			amcmMsgAppVo = new AmcmMsgAppVo();
			amcmMsgAppVo=amcmMsgAppService.getAmcmMsgApp(uuid);
			//修改为已读
			amcmMsgAppService.updateIsRead(uuid);
		}
		return amcmMsgAppVo;
	}
	/**
	 * 通过uuid删除数据
	 * @param uuid
	 * @retur
	 */
	@RequestMapping(value = "deleteById",method = RequestMethod.GET)
	@ResponseBody
	public String deleteById(String uuid){
			if(StringUtils.isNotEmpty(uuid)){
				
				amcmMsgAppService.deleteById(uuid);
				return JsonSuccess.factory("删除消息成功");
			}else{
				return JsonFailure.factory("未选择条目！");
			}
		
	}
}
